package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.Bed;
import com.cpj.bsyyzx.entity.BedRecord;
import com.cpj.bsyyzx.entity.User;
import com.cpj.bsyyzx.service.IBedRecordService;
import com.cpj.bsyyzx.service.IBedService;
import com.cpj.bsyyzx.service.IUserService;
import com.cpj.bsyyzx.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/bedRecord")
public class BedRecordController {
    @Autowired
    IBedRecordService bedRecordService;
    @Autowired
    IBedService bedService;
    @Autowired
    IUserService userService;

    @RequestMapping("list")
    CommonResult list(ModelMap map, Page page, @ModelAttribute("bed_record") BedRecord bedRecord) {
        page = bedRecordService.pageList(page, bedRecord);
        return CommonResult.success(page);
    }

    @RequestMapping("save")
    CommonResult save(BedRecord bedRecord) {
        if (saveDoing(bedRecord)){
            return CommonResult.success(true);
        }else {
            return CommonResult.failed("床位不存在或者已经被占用");
        }
    }

    public boolean saveDoing(BedRecord bedRecord){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bed_num",bedRecord.getBedInformation());
        Bed bed=bedService.getOne(wrapper);
        if(bed==null){
            return  false;
        }
        if (!("0".equals(bed.getStatus()))){
            return false;
        }
        bed.setStatus("1");
        bedService.saveOrUpdate(bed);
        return bedRecordService.saveOrUpdate(bedRecord);
    }

    @RequestMapping("update")
    CommonResult update(BedRecord bedRecord1){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("file_num",bedRecord1.getRecordNum());
        User user=userService.getOne(wrapper);
        user.setBedNumber(bedRecord1.getBedInformation());
        userService.updateById(user);
        return CommonResult.success(updateDoing(bedRecord1));
    }

    boolean updateDoing(BedRecord bedRecord1){
        BedRecord bedRecord=bedRecordService.getById(bedRecord1.getBedId());
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bed_num",bedRecord.getBedInformation());
        Bed bed=bedService.getOne(wrapper);
        bed.setStatus("0");
        bedService.saveOrUpdate(bed);

        wrapper=new QueryWrapper();
        wrapper.eq("bed_num",bedRecord1.getBedInformation());
        bed=bedService.getOne(wrapper);
        if ("离开".equals(bedRecord1.getState())){
            bed.setStatus("2");
        }
        else if ("退住".equals(bedRecord1.getState())){
            bed.setStatus("3");
        }else {
            bed.setStatus("1");
        }
        bedService.saveOrUpdate(bed);
        return bedRecordService.saveOrUpdate(bedRecord1);
    }

    @RequestMapping("getById")
    CommonResult getById(Integer id){
        return CommonResult.success(bedRecordService.getById(id));
    }
    @RequestMapping("delete")
    public CommonResult delete(Integer bedId){
        return CommonResult.success(deleteDoing(bedId));
    }
    boolean deleteDoing(Integer bedId){
        BedRecord bedRecord=bedRecordService.getById(bedId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bed_num",bedRecord.getBedInformation());
        Bed bed=bedService.getOne(wrapper);
        bed.setStatus("0");
        bedService.saveOrUpdate(bed);
        return bedRecordService.removeById(bedId);
    }



    @RequestMapping("getOneByBedNum")
    CommonResult getOneByBedNum(String bedNum){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bed_information",bedNum);
        return CommonResult.success(bedRecordService.list(wrapper));
    }
}
